//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-10-31  17-13-05 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-10-31  17-13-05 
//===============================





using UnityEngine;

namespace ZS.U3D.EXPAND.Detector
{
    [RequireComponent(typeof(MeshCollider))]
    [ExecuteInEditMode]
    public class FOVCollider : MonoBehaviour
    {
        [Range(0f, 50f)]
        public float Length;
        [Range(0f, 10f)]
        public float BaseSize;
        [Range(1f, 180f)]
        public float FOVAngle;
        [Range(1f, 180f)]
        public float ElevationAngle;
        [Range(0, 10)]
        public int Resolution;

        // Returns the generated collider mesh so that it can be rendered.
        private FOVMesh _fovMesh;
        public FOVMesh FOVMesh 
        { 
            get 
            {
                if (_fovMesh==null)
                {
                    _fovMesh = new FOVMesh();
                }
                return _fovMesh;
            } 
        }

        private MeshCollider _mc;

        private void Awake()
        {
            _mc = GetComponent<MeshCollider>();
            FOVMesh.UpdateFOVMesh(Length, BaseSize, FOVAngle, ElevationAngle, Resolution);
        }


        private void OnEnable(){} //Show Enable


        private void OnValidate()
        {

            FOVMesh.UpdateFOVMesh(Length, BaseSize, FOVAngle, ElevationAngle, Resolution);

            Length = FOVMesh.Length;
            BaseSize = FOVMesh.BaseSize;
            FOVAngle = FOVMesh.FOVAngle;
            ElevationAngle = FOVMesh.ElevationAngle;
            Resolution = FOVMesh.Resolution;

            if (_mc != null)
            {
                _mc.enabled = enabled;

                _mc.sharedMesh = _fovMesh.Mesh;
                _mc.convex = true;
                _mc.isTrigger = true;

            }
        }

        private void OnDrawGizmos()
        {
            FOVMesh.ShowGizmos(transform);
        }

    }
}
